import os
from .print import *
def makedirs(path, is_exit=True):
    os.makedirs(path, exist_ok=is_exit)

def makedirs_for_file(file_path, is_exit=True):
    dir_path = os.path.dirname(file_path)
    makedirs(dir_path, is_exit)

def exists(path):
    return os.path.exists(path)

def remove(path):
    os.remove(path)

def do_get_file_pure_name_from_path(path: str):
    """
    得到单纯的文件名，没有后缀和目录名
    不论名字有多少个点， 只吧最后一个点的右侧删去，保留左侧的内容，不保留最后一个点。
    """
    return os.path.splitext(os.path.basename(path))[0]



def do_get_file_size(file_path):
    """单位：MB"""
    if os.path.exists(file_path) is False:
        return 0
    return os.path.getsize(file_path) / (1024 ** 2)

def _copy_file_shell(source_path, destination_path):
    command_line = f"cp {source_path} {destination_path}"
    os.system(command_line)


def _copy_file_visualization(source_path, destination_path, buffer_size=64):
    buffer_size = buffer_size * 1024
    print_info(f'正在复制文件...从 {source_path} 到 {destination_path}')
    try:
        """"""
        with open(source_path, 'rb') as source_file, open(destination_path, 'wb') as destination_file:
            # 获取源文件的总大小，用于进度条的设置
            source_size = os.path.getsize(source_path)
            # 创建进度条
            with tqdm(total=source_size, unit='B', unit_scale=True, desc=f"正在复制") as pbar:
                # 流式复制文件内容，同时更新进度条
                while True:
                    # 读取缓冲区大小的内容
                    content = source_file.read(buffer_size)
                    if not content:
                        break
                    # 写入到目标文件
                    destination_file.write(content)
                    # 更新进度条
                    pbar.update(len(content))
        print(f"文件 {source_path} 已成功复制到 {destination_path}")
    except Exception as e:
        print(f"复制文件时发生错误：{e}")


def _copy_file_no_visualization(source_path, destination_path):
    makedirs(os.path.dirname(destination_path))
    print_info(f'正在复制文件...从 {source_path} 到 {destination_path}')
    try:
        with open(source_path, 'rb') as source_file:
            content = source_file.read()

        with open(destination_path, 'wb') as destination_file:
            destination_file.write(content)

        print(f"文件 {source_path} 已成功复制到 {destination_path}")
    except Exception as e:
        print(f"复制文件时发生错误：{e}")


def copy_file(source_path, destination_path, buffer_size=1024 * 6, use_shell=False, visualization=True, is_jump=False):
    assert isinstance(destination_path, str)
    if is_jump:
        if os.path.exists(destination_path):
            if do_get_file_size(destination_path) == do_get_file_size(source_path):
                print_info(f"文件已经存在,跳过复制操作：{destination_path}")
                return
    makedirs_for_file(destination_path)
    if use_shell:
        _copy_file_shell(source_path, destination_path)
    else:
        if visualization:
            #  6 * 1024 较佳， 750-800MB/s的传输速度, 但没有shell快，略逊于shell(6*1024)
            _copy_file_visualization(source_path, destination_path, buffer_size)
        else:
            _copy_file_no_visualization(source_path, destination_path)
